/* eslint-disable */
import Vue from 'vue'
import Vuex from 'vuex'
import axios from 'axios'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    list: [],
    inputValue: '',
    id: 4,
    statusKey: 'all'
  },
  mutations: {
    getList (state, data) {
      state.list = data
    },
    setInputValue (state, data) {
      state.inputValue = data
    },
    addValue (state) {
      const msg = { id: state.id, done: false, info: state.inputValue }
      state.list.push(msg)
      state.inputValue = ''
      state.id++
    },
    removeItem (state, id) {
      const index = state.list.findIndex(item => item.id === id)
      state.list.splice(index, 1)
    },
    changeCheckbox (state, param) {
      const index = state.list.findIndex(item => item.id === param.id)
      state.list[index].done = param.status
    },
    clear (state) {
      state.list = state.list.filter(item => item.done === false)
    },
    changeStatusKey(state,status){
      state.statusKey = status
    }
  },
  actions: {
    getListAsync (context) {
      axios.get('/todo.json').then((res) => {
        context.commit('getList', res.data)
      })
    }
  },
  modules: {
  },
  getters: {
    remain (state) {
      return state.list.filter(item => item.done === false).length
    },
    showList (state) {
      if (state.statusKey === 'all') {
        return state.list
      } else if (state.statusKey === 'nodo') {
        return state.list.filter(item => item.done === false)
      } else if (state.statusKey === 'done') {
        return state.list.filter(item => item.done === true)
      }
    }
  }
})
